WPF and Silverlight Edition Basic Library > TreeView > TreeView Quick Start > Step 2 of 3: Adding C1TreeViewItems |
In this step, you will see two different ways to add C1TreeViewItems to the C1TreeView control: in XAML markup and in code.
In XAML
To add static C1TreeViewItems to the C1TreeView control in the XAML:
XAML Copy Code<c1:C1TreeViewItem Header="Book List"></c1:C1TreeViewItem>
XAML Copy Code <c1:C1TreeViewItem Header="Language Books"/> <c1:C1TreeViewItem Header="Security Books"/>
XAML Copy Code <c1:C1TreeViewItem Header="Classic Books"> <c1:C1TreeViewItem Header="Catch-22"/> <c1:C1TreeViewItem Header="The Great Gatsby"/> </c1:C1TreeViewItem>You should have the following XAML markup now included in your file:
XAML Copy Code <Grid> <c1:C1TreeView Name="Tree" > <c1:C1TreeViewItem Header="Book List"> <c1:C1TreeViewItem Header="Language Books"/> <c1:C1TreeViewItem Header="Security Books"/> <c1:C1TreeViewItem Header="Classic Books"> <c1:C1TreeViewItem Header="Catch-22"/> <c1:C1TreeViewItem Header="The Great Gatsby"/> </c1:C1TreeViewItem> </c1:C1TreeViewItem> </c1:C1TreeView> </Grid>
In Code
To add static C1TreeViewItems to the C1TreeView control in the code behind file instead, add the following code in the Code Editor:
Visual Basic |
Copy Code
|
---|---|
Class MainWindow Public Sub New() InitializeComponent() InitializeTreeView() End Sub Private Sub InitializeTreeView() ' Remove items that were added at design time Tree.Items.Clear() Dim booklist As New C1TreeViewItem() booklist.Header = "Book List" Tree.Items.Add(booklist) ' Adding child items Dim language As New C1TreeViewItem() language.Header = "Language Books" booklist.Items.Add(language) ' Adding child items Dim security As New C1TreeViewItem() security.Header = "Security Books" booklist.Items.Add(security) ' Adding child items Dim classic As New C1TreeViewItem() classic.Header = "Classic Books" booklist.Items.Add(classic) ' Adding child items Dim subclassic As New C1TreeViewItem() subclassic.Header = "Catch-22" classic.Items.Add(subclassic) Dim subclassic2 As New C1TreeViewItem() subclassic2.Header = "The Great Gatsby" classic.Items.Add(subclassic2) End Sub End Class |
C# |
Copy Code
|
---|---|
public MainWindow() { InitializeComponent(); InitializeTreeView(); } void InitializeTreeView() { // Remove items that were added at design time Tree.Items.Clear(); C1TreeViewItem booklist = new C1TreeViewItem(); booklist.Header = "Book List"; Tree.Items.Add(booklist); // Adding child items C1TreeViewItem language = new C1TreeViewItem(); language.Header = "Language Books"; booklist.Items.Add( language ); // Adding child items C1TreeViewItem security = new C1TreeViewItem(); security.Header = "Security Books"; booklist.Items.Add(security); // Adding child items C1TreeViewItem classic = new C1TreeViewItem(); classic.Header = "Classic Books"; booklist.Items.Add(classic); // Adding child items C1TreeViewItem subclassic = new C1TreeViewItem(); subclassic.Header = "Catch-22"; classic.Items.Add(subclassic); C1TreeViewItem subclassic2 = new C1TreeViewItem(); subclassic2.Header = "The Great Gatsby"; classic.Items.Add(subclassic2); } |